package org.batatasframework.support.attribute;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 这个是对应从数据库读出来的一条指标的内容项
 * @author jackson
 *
 */
public class AttributeValue {
	
	protected Long id;
	
	protected String code;
	
	protected Double value;
	
	protected boolean deleted;
	
	protected Double oldValue;

	protected String strvalue;
	
	protected String oldStrvalue;
	
	public AttributeValue(){}
	
	public AttributeValue(AttributeValue copy){
		if(null != copy){
			this.id = copy.id;
			this.code = copy.code;
			this.value = copy.value;
			this.deleted = copy.deleted;
			this.oldValue = copy.oldValue;
		}
	}

	public AttributeValue(ResultSet rs) throws SQLException{
		this.id = rs.getLong(1);
		this.code = rs.getString(2);
		this.value = rs.getDouble(3);
		this.deleted = rs.getBoolean(4);
	}

	public AttributeValue(String code, ResultSet rs) throws SQLException{
		this.code = code;
		this.id = rs.getLong(1);
		this.value = rs.getDouble(3);
		this.deleted = rs.getBoolean(4);
	}
	
	public void insert(PreparedStatement ps, Long owner) throws SQLException{
		checkValue(value);
		
		ps.setLong(1, owner);
		ps.setString(2, code);
		ps.setDouble(3, value);
		ps.addBatch();
	}
	
	protected  void checkValue(Double value) {
		if(value.isNaN()|| value.isInfinite()){
			throw new RuntimeException("0/0！or N / 0");
		}
	}

	public void update(PreparedStatement ps) throws SQLException{
		checkValue(value);
		
		ps.setString(1, code);
		ps.setDouble(2, value);
		ps.setBoolean(3, deleted);
		ps.setLong(4, id);
		ps.addBatch();
	}
	
	public int intValue(){
		if(null == value) return 0;
		return value.intValue();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Double getValue() {
		return value;
	}

	public void setValue(Double value) {
		this.value = value;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public Double getOldValue() {
		return oldValue;
	}

	public void setOldValue(Double oldValue) {
		this.oldValue = oldValue;
	}
	
}
